package com.kdtech.analyse.video;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;
/**
 * http://tv.sohu.com/ 搜狐视频
 * @author allen 
 */
public class SohuAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://tv.sohu.com/.*/.*.shtml",
				"http://s.sohu.com/[0-9]{8}/n[0-9]*.shtml",
				"http://my.tv.sohu.com/.*/[0-9].*/[0-9].*.shtml"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {		
		NewsMeta video = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		video.setUrl(url);		
		String title = null;
		String desc = null;
		Long date = null;

		Document doc = Jsoup.parse(htmltxt);
		title =doc.select("div#vTitle.area > div.main-tit > h1").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("div#location.area > div.left > span").text();
			if(StringUtils.isBlank(title)){
				title=doc.select("title").html();
				title=StringUtils.substringBeforeLast(title, "-");
				title=title.replace("-新闻视频", "");
				if(StringUtils.isBlank(title)){
				}
			}
		}
		String tempdate=null;
		if(url.startsWith("http://tv.sohu.com/")){
			tempdate = url.replaceAll("http://tv.sohu.com/", "");
			if(tempdate.indexOf("/")!=-1){
				tempdate=tempdate.substring(0,tempdate.indexOf("/"));
				if(tempdate.length()>=8){
					String yearstr=tempdate.substring(0,4);
					String mstr=tempdate.substring(4,6);
					String dstr=tempdate.substring(6,8);
					tempdate=yearstr+"-"+mstr+"-"+dstr;
				}
			}
		}else{
			tempdate = StringUtils.substringBetween(htmltxt, "cover=\"", "\"");
			if(StringUtils.isBlank(tempdate)){
				tempdate=doc.select("html body div#contentB div.right div#info.infoBox div.user-photo div.txt").text();
			}
			if(StringUtils.isBlank(tempdate)){
				tempdate=doc.select("html body div#contentB div.left div#info.user div.user-info div.cfix div.r p").text();
			}
		}
		date=DateUtils.matchDate(tempdate);

		desc = doc.select("div.brief").text();
		video.setTitle(StringUtils.trimSpace(title));
		video.setContent(desc);
		video.setDate(date);	
		
		/**
		 * 解析用于更新的地址
		 */
		String updateUrl=null;
		if(htmltxt.indexOf("var vid=\"")!=-1){
			String videoId=htmltxt.substring(htmltxt.indexOf("var vid=\"")+"var vid=\"".length());
			if(videoId.indexOf("\"")!=-1){
				videoId=videoId.substring(0,videoId.indexOf("\""));
			
				updateUrl="http://count.vrs.sohu.com/count/stat.do?t=1353924728750.6082&videoId="+videoId;				
				video.setUpdateUrl(updateUrl);
				NewsMeta update = Update(video);
				if(update!=null){
					video.setCommentNum(update.getCommentNum());
					video.setClickNum(update.getClickNum());
				 }
			}
		}
	    return video;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if(responseToURL!=null){
					String html = responseToURL.getHtml();			
					if(html!=null&& html.indexOf("=")!=-1){
						String clickNumstr=html.substring(html.indexOf("=")+1);							
						clickNumstr=clickNumstr.replaceAll(" ", "");
						Integer clickNum=0;
						try {
							clickNum=Integer.parseInt(clickNumstr);
						} catch (Exception e) {
							// TODO: handle exception
						}						
						meta.setClickNum(clickNum);
						return meta;
					}				
				}
			}
		}	
		return null;
	}
	public static void main(String[] args) throws Exception {
		SohuAnalyse a = new SohuAnalyse();
		String url = "http://tv.sohu.com/20150629/n415805457.shtml";
		System.out.println(a.isDetailPage(url));
		UrlMeta meta = CrawlHTML.responseToURL(url);
		NewsMeta parserHtml = a.parserHtml(meta);
		System.out.println(parserHtml);
//		String str="%7B%22videoid%22%3A%22120023337%22%2C%22userid%22%3A%2238249615%22%2C%22oldSid%22%3A-1%7D";
//		System.out.println(java.net.URLDecoder.decode(str, "utf-8"));
	}

	
	
}
